package com.wtwd.device.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.dao.AppDeviceHeartRateTotalMapper;
import com.wtwd.device.model.dto.DeviceHeartRateTotalDto;
import com.wtwd.device.model.entity.DeviceHeartRate;
import com.wtwd.device.model.entity.DeviceHeartRateTotal;
import com.wtwd.device.model.vo.DeviceHeartRateTotalVo;
import com.wtwd.device.model.vo.HeartRateVo;
import com.wtwd.device.service.AppDeviceHeartRateService;
import com.wtwd.device.service.AppDeviceHeartRateTotalService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-05-27
 */
@Service
public class AppDeviceHeartRateTotalServiceImpl extends ServiceImpl<AppDeviceHeartRateTotalMapper, DeviceHeartRateTotal> implements AppDeviceHeartRateTotalService {


    @Resource
    private AppDeviceHeartRateTotalMapper appDeviceHeartRateTotalMapper;

    @Resource
    private AppDeviceHeartRateService appDeviceHeartRateService;


    @Override
    public Boolean insertOrUpdate(DeviceHeartRateTotal heartRateTotal) {
        if (heartRateTotal == null) {
            return false;
        }
        return appDeviceHeartRateTotalMapper.insertOrUpdate(heartRateTotal) > 0 ? true : false;
    }

    @Override
    public DeviceHeartRateTotal getDeviceHeartRateTotal(Long deviceId, long time) {
        return new LambdaQueryChainWrapper<>(appDeviceHeartRateTotalMapper)
                .eq(DeviceHeartRateTotal::getDeviceId, deviceId)
                .eq(DeviceHeartRateTotal::getTime, time)
                .one();
    }

    @Override
    public List<DeviceHeartRateTotal> getDaysHeartRate(Long deviceId, String startTime, String endTime) {
        return new LambdaQueryChainWrapper<>(appDeviceHeartRateTotalMapper)
                .eq(DeviceHeartRateTotal::getDeviceId, deviceId)
                .between(DeviceHeartRateTotal::getTime, startTime, endTime)
                .list();
    }

    @Override
    public DeviceHeartRateTotalVo getDayHeartRateByTime(Long deviceId, Long id, String time) {
        DeviceHeartRateTotal deviceHeartRateTotal = getDayDeviceHeartRateTotalByTime(deviceId, time);
        if (deviceHeartRateTotal == null) {
            return null;
        }
        DeviceHeartRateTotalVo deviceHeartRateTotalVo = new DeviceHeartRateTotalVo();
        List<DeviceHeartRate> deviceHeartRates = appDeviceHeartRateService.getDayHeartRateByTotalIdGtId(deviceHeartRateTotal.getId(), id);
        BeanUtils.copyProperties(deviceHeartRateTotal, deviceHeartRateTotalVo);
        deviceHeartRateTotalVo.setDeviceHeartRates(deviceHeartRates);
        return deviceHeartRateTotalVo;
    }

    @Override
    public DeviceHeartRateTotal getDayDeviceHeartRateTotalByTime(Long deviceId, String time) {
        return new LambdaQueryChainWrapper<>(appDeviceHeartRateTotalMapper)
                .eq(DeviceHeartRateTotal::getDeviceId, deviceId)
                .eq(DeviceHeartRateTotal::getTime, time)
                .one();
    }

    @Override
    public List<DeviceHeartRateTotal> synchronizationHeartRate(List<DeviceHeartRateTotalDto> deviceHeartRateTotalDtos) {
        List<DeviceHeartRateTotal> deviceHeartRateTotals = new ArrayList<>();
        for (DeviceHeartRateTotalDto deviceHeartRateTotalDto : deviceHeartRateTotalDtos) {
            DeviceHeartRateTotal deviceHeartRateTotal = new DeviceHeartRateTotal();
            BeanUtils.copyProperties(deviceHeartRateTotalDto, deviceHeartRateTotal);
            DeviceHeartRateTotal heartRateTotal = getDayDeviceHeartRateTotalByTime(deviceHeartRateTotalDto.getDeviceId(), deviceHeartRateTotalDto.getTime());
            if (heartRateTotal == null) {//插入
                save(deviceHeartRateTotal);
            } else {
                deviceHeartRateTotal.setId(heartRateTotal.getId());
            }
            List<DeviceHeartRate> deviceHeartRates = deviceHeartRateTotalDto.getDeviceHeartRates();
            if (deviceHeartRates != null && deviceHeartRates.size() > 0) {
                deviceHeartRates.stream().forEach(e -> {
                    e.setDeviceHeartRateTotalId(deviceHeartRateTotal.getId());
                });
                appDeviceHeartRateService.saveBatch(deviceHeartRates);
                DeviceHeartRate deviceHeartRate = appDeviceHeartRateService.getLastDeviceHeartRateByTotalId(deviceHeartRateTotal.getId());
                deviceHeartRateTotal.setLastInfoId(deviceHeartRate.getId());
                updateById(deviceHeartRateTotal);
            }
            deviceHeartRateTotals.add(deviceHeartRateTotal);
        }
        return deviceHeartRateTotals;
    }

    @Override
    public boolean bathInsertOrUpdate(List<DeviceHeartRateTotal> deviceHeartRateTotals) {
        if (deviceHeartRateTotals == null || deviceHeartRateTotals.size() == 0) {
            return false;
        }
        return appDeviceHeartRateTotalMapper.bathInsertOrUpdate(deviceHeartRateTotals) > 0 ? true : false;
    }

    @Override
    public HeartRateVo selectHeartRateByTime(Long deviceId, String time) {
        return appDeviceHeartRateTotalMapper.selectHeartRateByTime(deviceId, time);
    }
}
